<?php
namespace Home\Controller;

use Vendor\Page;

class RankController extends ComController
{

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        //点击榜
        $click_rank = D('novel')->field('click_count,name,writer')->limit(3)->order('click_count desc')->select();
        $this->assign('click_rank', $click_rank);
        //畅销榜
        $hot_rank = D('')->query("select count(1) as count,b.writer,a.novel_name from qw_read_history as a left join qw_novel as b on b.id = a.novel_id group by novel_id order by count desc limit 3");
        $this->assign('hot_rank', $hot_rank);
        //打赏榜
        $dashang_rank = D('')->query("select count(1) as count,b.writer,b.name from qw_dashang_history as a left join qw_novel as b on b.id = a.novel_id group by novel_id order by count desc limit 3");
        $this->assign('dashang_rank', $dashang_rank);
        //留存榜
        $coll_rank = D('')->query("select count(1) as count,b.writer,b.name from qw_bookshelf as a left join qw_novel as b on b.id = a.novel_id group by novel_id order by count desc limit 3");
        $this->assign('coll_rank', $coll_rank);
        /*//本周潜力榜
        $poten_rank = D('')->query("select count(1) as count,b.writer,a.novel_name from qw_bookshelf as a left join qw_novel as b on b.id = a.novel_id where WEEK(FROM_UNIXTIME(addtime, '%Y-%m-%d %T')) = WEEK(NOW()) group by novel_id order by count desc limit 3");
        $this->assign('poten_rank', $poten_rank);*/
        $this->display();
    }

    public function ranking_info(){
        if($_GET['cate'] == 1){
            $ranking_info = D('novel')->field('id,click_count,description,name,img,writer')->order('click_count desc')->limit(10)->select();
            $title = '点击榜';
            $type = 'djb';
        }elseif ($_GET['cate'] == 2){
            $ranking_info = D('')->query("select count(1) as count,b.writer,b.name,b.img,b.description,b.id from qw_read_history as a left join qw_novel as b on b.id = a.novel_id group by novel_id order by count desc limit 10");
            $title = '畅销榜';
            $type = 'cxb';
        }elseif ($_GET['cate'] == 3){
            $ranking_info = D('')->query("select count(1) as count,b.writer,b.name,b.img,b.description,b.id from qw_dashang_history as a left join qw_novel as b on b.id = a.novel_id group by novel_id order by count desc limit 10");
            $title = '打赏榜';
            $type = 'dsb';
        }elseif ($_GET['cate'] == 4){
            $ranking_info = D('')->query("select count(1) as count,b.writer,b.name,b.img,b.description,b.id from qw_bookshelf as a left join qw_novel as b on b.id = a.novel_id group by novel_id order by count desc limit 10");
            $title = '留存榜';
            $type = 'lcb';
        }
        $this->assign('ranking_info',$ranking_info);
        $this->assign('title',$title);
        $this->assign('type',$type);
        $this->display();
    }

    /**
     * ajax获取图书列表
     */
    public function ajax_book_list()
    {

        if ($_POST['type'] == 'djb') {
            $djb_count = D('novel')->count();
            $page = $_POST['p'] * 10;
            if($page<$djb_count && $page<=90){
                $ranking_info = D('novel')->field('id,click_count,description,name,img,writer')->order('click_count desc')->limit($page, 10)->select();
                $this->ajax_book_html($ranking_info);
            }
        } elseif ($_POST['type'] == 'dsb') {
            $dsb_count = D('dashang_history')->group('novel_id')->count();
            $page = $_POST['p'] * 10;
            if($page<$dsb_count) {
                $ranking_info = D('')->query("select count(1) as count,b.writer,b.name,b.img,b.description from qw_dashang_history as a left join qw_novel as b on b.id = a.novel_id group by novel_id order by count desc limit 10");
                $this->ajax_book_html($ranking_info);
            }
        } elseif ($_POST['type'] == 'lcb') {
            $lcb_count = D('bookshelf')->group('novel_id')->count();
            $page = $_POST['p'] * 10;
            if($page<$lcb_count) {
                $ranking_info = D('')->query("select count(1) as count,b.writer,b.name,b.img,b.description from qw_bookshelf as a left join qw_novel as b on b.id = a.novel_id group by novel_id order by count desc limit 10");
                $this->ajax_book_html($ranking_info);
            }

        }
    }
    public function ajax_book_html($v){
        $file_path = C('FILE_HOST_PATH');
        foreach ($v as $key => $value) {
            $id = $value['id'];
            $img_path = $file_path . $value['img'];
            $name = $value['name'];
            $writer = $value['writer'] ? $value['writer'] : '佚名';
            $dewcription = $value['description'];
            $data['data'] .= "<div class='item'>
					<a href='/Home/Novel/novelinfo/id/$id/'><img src='$img_path' class='avatar'>
						<div class='body'><span class='t'>$name</span><span class='author'>$writer</span><span class='btn'>立即阅读</span>
							<p>$dewcription</p>
						</div>
					</a>
				</div>";
        }
        $data['status'] = 1;
        $data['info'] = '成功';
        $this->ajaxReturn($data);
    }
}
    